<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Bootloader Development</title>
		<link rel="stylesheet" type="text/css" href="./images/style.css">
	</head>
	
	<body>
		<header class="banner">
			<h1>PIC18系列 MCU 串口自举程序（Bootloader）开发</h1>
		</header>
		
		<main>
			<section>
				<h2>1. 简介</h2>
				<article>
					<p>本示例为基于MCC环境快速开发的PIC18系列MCU(PIC18F47Q10)的串口自举程序。</p>
				</article>
			</section>
			
			<section>
				<h2>2. 工具</h2>
				<article>
					<header>
					<h3>2.1. 软件开发环境</h3>
					</header>
					<p>相关软件开发版本情况说明如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/mplabx" target="_blank">Microchip MPLAB® xIDE version 5.50 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/mplab-xc-compilers" target="_blank">Microchip MPLAB® XC8 C compiler version 2.32 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) version 4.2.1 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) Device Libraries PIC10 / PIC12 / PIC16 / PIC18 MCUs version 1.81.7 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip 18-bit Bootloader version 2.4.1 or newer</a></li>
					<li><a href="https://packs.download.microchip.com/" target="_blank">Microchip PIC18F-Q Series Device Support version 1.12.193 or newer</a></li>
					</ul>
				</article>
				<article>
					<header>
					<h3>2.2. 硬件开发环境</h3>
					</header>
					<p>该例程基于如下硬件开发。</p>
					<ul>
					<li><a href="https://www.microchip.com/developmenttools/ProductDetails/DM164136" target="_blank">Curiosity High Pin Count (HPC) Development Board (Part Number: DM164136)</a></li>
					<img src="./images/201018-MCU8-PHOTO-DM164136_Curiosity-HPC-Angle-mD.jpg" width="15%"; style="margin-left: 40px;">
					<li><a href="https://www.microchip.com/en-us/product/PIC18F47Q10#buy-from-store" target="_blank">PIC18F47Q10-I/P</a></li>
					<img src="./images/medium-PIC18F47Q10-PDIP-40.png" width="10%"; style="margin-left: 40px;">
					<li><a href="https://www.microchip.com/en-us/development-tool/ADM00393" target="_blank">MCP2200 BREAKOUT MODULE (Part Number: ADM00393)</a></li>
					<img src="./images/814-adm00393.jpg" width="10%"; style="margin-left: 40px;">
					</ul>
				</article>
				<article>
					<header>
					<h3>2.3. 参考资料</h3>
					</header>
					<p>相关参考资料如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/product/PIC18F47Q10" target="_blank">PIC18F47Q10 Data Sheet</a></li>
					<li><a href="https://ww1.microchip.com/downloads/en/DeviceDoc/Curiosity-High-Pin-Count-Development-Board-User-Guide-40001856C.pdf" target="_blank">Curiosity High Pin Count (HPC) Development Board User's Guide</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tool/ADM00393" target="_blank">MCP2200 Breakout Module User's Guide</a></li>
					<li><a href="https://ww1.microchip.com/downloads/en/DeviceDoc/40001779B.pdf" target="_blank">Bootloader Generator User's Guide</a></li>
					</ul>
				</article>
			</section>
			
			<section>
				<h2>3. 自举程序工程创建</h2>
				<article>
					<header>
						<h3>3.1. 打开MPLAB® xIDE，选择"File->New Project"。</h3>
					</header>
					<img src="./images/boot_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.2. 选择"Standalone Project"，点击下一步。</h3>
					</header>
					<img src="./images/boot_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.3. 输入器件型号，点击下一步。</h3>
					</header>
					<img src="./images/boot_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.4. 选择编译器版本，点击下一步。</h3>
					</header>
					<img src="./images/boot_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.5. 输入工程项目名称，并选择相应存取路径后点击完成按钮，完成空工程的创建。</h3>
					</header>
					<img src="./images/boot_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.6. 选择"Tools->Embedded->MPLAB Code Configurator v4: Open/Close"选项，打开MCC开发环境。</h3>
					</header>
					<img src="./images/boot_setup_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.7. MCC打开后，将MCC内核版本从当前的v5.2.3到v5.0.2。</h3>
					</header>
					<img src="./images/boot_setup_7.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.8. 在System Module下按如下设置系统时钟。</h3>
					</header>
					<img src="./images/boot_setup_8.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.9. 在器件资源列表里添加8位bootloader generator库到项目资源列表中，并按如下设置。</h3>
					</header>
					<img src="./images/boot_setup_9.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.10. 添加外设资源MEMORY和EUSART2到项目资源列表中，这里检查一下EUSART2的配置情况，采用该默认配置即可。</h3>
					</header>
					<img src="./images/boot_setup_10.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.11. 按下图对相关管脚进行设置，其中RD1是串口接收引脚、RD0是串口发送引脚；RB4是好奇板的S1按键，RA7是好奇板的D5。</h3>
					</header>
					<img src="./images/boot_setup_11.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.12. 点击"Generate"按钮，生成代码，弹出的确认框点击"Yes"。</h3>
					</header>
					<img src="./images/boot_setup_12.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.13. 点击下图编译按钮，会发现编译失败，并提示相关告警和错误。</h3>
					</header>
					<img src="./images/boot_setup_13.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.14. 首先对pic18f_uart.c文件做如下修改，左侧为修改前文件，右侧为修改后文件。</h3>
					</header>
					<img src="./images/boot_setup_14.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.15. 接着对pic18f_bootload.c文件做如下修改，左侧为修改前文件，右侧为修改后文件。注意，此处显示不全，具体内容大家可以直接看工程文件。</h3>
					</header>
					<img src="./images/boot_setup_15.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.16. 点击下图编译按钮，发现经上述修改后自举程序工程可成功编译。</h3>
					</header>
					<img src="./images/boot_setup_16.png" width="80%">
				</article>

				<article>
					<header>
						<h3>3.17. 设置自举程序工程代码空间范围。</h3>
					</header>
					<img src="./images/boot_setup_17.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.18. 在项目属性里点击"Manage Configurations..."，准备进行项目属性配置。</h3>
					</header>
					<img src="./images/boot_setup_18.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.19. 点击“Duplicate”按钮复制默认项目属性配置信息。</h3>
					</header>
					<img src="./images/boot_setup_19.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.20. 点击“Rename”按钮分别重命名为“With_Configurations”和“No_Configurations”。</h3>
					</header>
					<img src="./images/boot_setup_20.png" width="80%">
				</article>

				<article>
					<header>
						<h3>3.21. 在“No_Configurations”的配置下，点击XC8 Complier后添加宏定义"OMIT_CONFIGURATIONS"。</h3>
					</header>
					<img src="./images/boot_setup_21.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.22. 对device_config.c源文件进行修改，右侧为更改后文件，意思为当定义了宏"OMIT_CONFIGURATIONS"，则不编译如下配置字。</h3>
					</header>
					<img src="./images/boot_setup_22.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.23. 在“No_Configurations”的配置下，点击XC8 Linker，进一步去掉默认配置字的生成，确保在“No_Configurations”的配置下不会生成配置字信息，以便与应用程序代码合并hex。自此自举程序工程开发完毕。</h3>
					</header>
					<img src="./images/boot_setup_23.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>4. 应用程序工程创建</h2>
				<article>
					<header>
						<h3>4.1. 选择"File->New Project"。</h3>
					</header>
					<img src="./images/app_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.2. 选择"Standalone Project"，点击下一步。</h3>
					</header>
					<img src="./images/app_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.3. 输入器件型号，点击下一步。</h3>
					</header>
					<img src="./images/app_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.4. 选择编译器版本，点击下一步。</h3>
					</header>
					<img src="./images/app_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.5. 输入工程项目名称，并选择相应存取路径后点击完成按钮，完成空工程的创建。</h3>
					</header>
					<img src="./images/app_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.6. 选择"Tools->Embedded->MPLAB Code Configurator v4: Open/Close"选项，打开MCC开发环境。</h3>
					</header>
					<img src="./images/app_setup_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.7. 在System Module下按如下设置系统时钟。</h3>
					</header>
					<img src="./images/app_setup_7.png" width="80%">
				</article>
				<article>
				<article>
					<header>
						<h3>4.8. 配置字做如下修改，确保应用程序工程的配置字和自举程序工程严格一致。</h3>
					</header>
					<img src="./images/app_setup_8.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.9. 按下图对相关管脚进行设置，其中RA5是好奇板的D3，RA6是好奇板的D4。</h3>
					</header>
					<img src="./images/app_setup_9.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.10. 添加外设资源TMR0到项目资源列表中，并按如下设置。该TMR0用于实现好奇板D3的500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_10.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.11. 点击"Generate"按钮，生成代码。</h3>
					</header>
					<img src="./images/app_setup_11.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.12. 对main.c文件做如下更改，其中右侧为更改后文件内容。使能中断，TMR0_UserOverflowCallback()函数用于D3 500ms闪烁，在while(1)循环中实现D4的500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_12.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.13. 在项目属性里，点击XC8 Complier, 添加头文件路径“mcc_generated_files\timer”，否则会因头文件路径不正确而编译失败。</h3>
					</header>
					<img src="./images/app_setup_13.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.14. 同样复制三个项目配置，分别命名为“Offset”，“Combined”和“StandAlone”。</h3>
					</header>
					<img src="./images/app_setup_14.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.15. 在“Offset”配置下，设置应用程序工程代码的地址偏移，此偏移根据自举程序工程代码空间范围而定。</h3>
					</header>
					<img src="./images/app_setup_15.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.16. 同样在“Combined”配置下，设置相同的应用程序工程代码的地址偏移。</h3>
					</header>
					<img src="./images/app_setup_16.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.17. 在“Combined”配置下的Loading下，添加自举程序工程。</h3>
					</header>
					<img src="./images/app_setup_17.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.18. 添加的自举程序工程配置信息要选择“No_Configurations”,之后点击“OK”按钮完成应用程序工程开发。</h3>
					</header>
					<img src="./images/app_setup_18.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>5. 串口升级实验验证</h2>
				<article>
					<header>
						<h3>5.1. 桌面实验平台搭建如下。</h3>
					</header>
					<img src="./images/demo.png" width="40%">
				</article>
				<article>
					<header>
						<h3>5.2. 将应用程序的项目配置切换为“Combined”，选择板上PKOB4，下载应用程序，程序烧录后可以看到D3和D4 500ms闪烁。</h3>
					</header>
					<img src="./images/verify_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.3. 将应用程序的项目配置切换为“Offset”，按下下图编译按钮完成编译。</h3>
					</header>
					<img src="./images/verify_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.4. 同时按住好奇开发板的MCLR按键和S1按键，然后先释放MCLR按键，再释放S1按键即可进入boot模式，D5常亮。接着明确MCP2200串口号，对scripts文件夹下的serial_upload.bat进行相应修改，之后点击该批处理文件完成升级工作。升级成功后，D3和D4以500ms闪烁。</h3>
					</header>
					<img src="./images/verify_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.5. 若需要再次升级，可以重复5.4步骤。</h3>
					</header>
				</article>
			</section>
		</main>
	</body>
</html>